package Duoxiancheng;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Thread123 {
    static AtomicInteger num = new AtomicInteger(0);
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(new MineThread(0,"1")).start();
        new Thread(new MineThread(1,"2")).start();
        new Thread(new MineThread(2,"3")).start();
    }

    static class MineThread implements Runnable{
        int state;
        String str;

        public MineThread(int state,String str){
            this.state = state;
            this.str = str;
        }

        @Override
        public void run() {
            while (num.get()<100){
                try {
                    lock.lock();
                    if(num.get()%3==state){
                        num.incrementAndGet();
//                        System.out.println(Thread.currentThread().getName()+" get lock");
                        System.out.println(str);
                    }
                }finally {
//                    System.out.println(Thread.currentThread().getName()+"unlock");
                    lock.unlock();
                }
            }

        }
    }

}
